﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Mysoft.Common.Configuration
{
    /// <summary>
    /// appsettings配置类
    /// </summary>
    public static class AppSetting
    {
        /// <summary>
        /// IConfiguration对象
        /// </summary>
        public static IConfiguration Configuration { get; private set; }

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public static string DbConnectionString
        {
            get { return _connection.DbConnectionString; }
        }
        /// <summary>
        /// 数据库类型/sqlservice/mysql/postgresql
        /// </summary>
        public static string DBType
        {
            get { return _connection.DBType; }
        }
        /// <summary>
        /// Redis连接字符串
        /// </summary>
        public static string RedisConnectionString
        {
            get { return _connection.RedisConnectionString; }
        }
        /// <summary>
        /// 是否启用Redis缓存
        /// </summary>
        public static bool UseRedis
        {
            get { return _connection.UseRedis; }
        }
        /// <summary>
        /// MQ连接字符串
        /// </summary>
        public static string MQConnectionString
        {
            get { return _connection.MQConnectionString; }
        }
        /// <summary>
        /// 是否启用MQ
        /// </summary>
        public static bool UseRabbitMQ
        {
            get { return _connection.UseRabbitMQ; }
        }

        private static Connection _connection;
        /// <summary>
        /// jwt
        /// </summary>
        public static JWTToken JWTToken { get; private set; }

        /// <summary>
        /// TokenHeaderName
        /// </summary>
        public static string TokenHeaderName = "Authorization";

        public static void Register(IServiceCollection services, IConfiguration configuration)
        {
            Configuration = configuration;
            services.Configure<Connection>(configuration.GetSection("Connection"));
            services.Configure<JWTToken>(configuration.GetSection("JWTToken"));
            var provider = services.BuildServiceProvider();
            JWTToken = provider.GetRequiredService<IOptions<JWTToken>>().Value;
            _connection = provider.GetRequiredService<IOptions<Connection>>().Value;
            if (string.IsNullOrWhiteSpace(_connection.DbConnectionString))
                throw new System.Exception("未配置好数据库默认连接");
        }
    }


    public class Connection 
    {
        public string DBType { get; set; }
        public string DbConnectionString { get; set; }
        public string RedisConnectionString { get; set; }
        public bool UseRedis { get; set; }
        public string MQConnectionString { get; set; }
        public bool UseRabbitMQ { get; set; }
    }
    public class JWTToken 
    {
        public string Issuer { get; set; }
        public string Audience { get; set; }
        public string Secret { get; set; }
        public int AccessExpiration { get; set; }

        public int ClockSkew { get; set; }

    }
}
